﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ASelect.Service.Contracts;
using ASelect;

namespace ASelect.Service
{
    /// <summary>
    /// The implementation of the IASelectService interface.
    /// </summary>
    [ServiceBehavior(Namespace = "http://localhost/Services/ASelect", Name = "ASelect")]
    public class ASelectService : IASelectService
    {
        /// <summary>
        /// Check the login details and return the result.
        /// </summary>
        /// <param name="number">The numer of the entity</param>
        /// <param name="passwordHash">The password hash</param>
        /// <returns>A value from the LoginResult enumeration</returns>
        public LoginResult Login(string number, string passwordHash)
        {
            /* define the result */
            LoginResult result = LoginResult.USERNAME_ERROR;

            /* connect to data layer */
            ASelectData data = new ASelectData();

            /* log in */
            ASelectData.LoginState loginResult = data.Login(number, passwordHash);

            /* fill LoginResult */
            switch (loginResult)
            {
                case ASelectData.LoginState.SUCCESSFUL: 
                    result = LoginResult.SUCCESSFUL;
                    break;
                case ASelectData.LoginState.USERNAME_ERROR:
                    result = LoginResult.USERNAME_ERROR;
                    break;
                case ASelectData.LoginState.PASSWORD_ERROR:
                    result = LoginResult.PASSWORD_ERROR;
                    break;
                case ASelectData.LoginState.USERNAME_EMPTY:
                    result = LoginResult.USERNAME_EMPTY;
                    break;
                case ASelectData.LoginState.PASSWORD_EMPTY:
                    result = LoginResult.PASSWORD_EMPTY;
                    break;
                default:
                    result = LoginResult.USERNAME_ERROR;
                    break;
            }

            /* return the result */
            return result;
        }

        /// <summary>
        /// Fetch the details of an account from the database.
        /// </summary>
        /// <param name="number">The number of the entity</param>
        /// <returns>A filled AccountDetails object</returns>
        public AccountDetails GetAccountDetails(string number)
        {
            /* define result */
            AccountDetails result = new AccountDetails();

            /* connect to data layer */
            ASelectData data = new ASelectData();

            /* find the entity with the given number */
            Account account = data.GetAccount(number);

            /* if an entity was found */
            if (account != null)
            {
                /* create RoleDetails object */
                RoleDetails roleDetails = new RoleDetails();

                /* fill the RoleDetails */
                roleDetails.Id = account.Role1.Id;
                roleDetails.Description = account.Role1.Description;

                /* fill the AccountDetails */
                result.Number = account.Number;
                result.LastLogin = (DateTime)account.LastLogin;
                result.Role = roleDetails;
            }

            /* return result */
            return result;
        }
    }
}
